<?php
/**
 * Description:
 *
 * @author      Eden
 * @datetime    2017/2/7 11:30
 * @copyright   WuHan HuQi Technology Co.,Ltd.
 * @url         http://cnhuqi.com
 */

class Summary_Model extends ADM_Model
{
    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function getTotal()
    {

        $adminid = adminId();
        if ($adminid == 2) {

            $userdb = clone($this->db);
            $userdb->where('virtual', 0);
            $users = $userdb->count_all_results('user');


            $orderdb = clone($this->db);
            $orderdb->where('virtual', 0);
            $order = $orderdb->count_all_results('user_order');

            $db = clone($this->db);
            $base = $db->count_all('base');

            $product = $db->count_all('base_goods');

            $db->select_sum('bbsIncr');
            $db->select_sum('installTimes');
            $query = $db->get('analytics_daily');
            $bbsSum = $query->row_array();
            $bbs = $bbsSum['bbsIncr'];
            $installTimes = $bbsSum['installTimes'];

            $query = $db->get_where('analytics_daily', ['date' => date('Y-m-d')]);
            $row = $query->row_array();
            $pv = !empty($row) ? $row['pv'] : 0;
            $startup = !empty($row) ? $row['startUpTimes'] : 0;
        } else {
            $db = clone($this->db);
            $users = $db->count_all('user');
            $order = $db->count_all('user_order');
            $base = $db->count_all('base');
            $product = $db->count_all('base_goods');
            $db->select_sum('bbsIncr');
            $db->select_sum('vBbsIncr');
            $db->select_sum('installTimes');
            $db->select_sum('vInstallTimes');
            $query = $db->get('analytics_daily');
            $bbsSum = $query->row_array();
            $bbs = $bbsSum['bbsIncr'] + $bbsSum['vBbsIncr'];
            $installTimes = $bbsSum['installTimes'] + $bbsSum['vInstallTimes'];

            $query = $db->get_where('analytics_daily', ['date' => date('Y-m-d')]);
            $row = $query->row_array();
            $pv = !empty($row) ? $row['pv'] + $row['vPv'] : 0;
            $startup = !empty($row) ? $row['startUpTimes'] + $row['vStartUpTimes'] : 0;
        }

        $data = [
            'order' => !empty($order) ? intval($order) : 0,
            'product' => !empty($product) ? intval($product) : 0,
            'user' => !empty($users) ? intval($users) : 0,
            'bbs' => !empty($bbs) ? intval($bbs) : 0,
            'install' => !empty($installTimes) ? intval($installTimes) : 0,
            'pv' => !empty($pv) ? intval($pv) : 0,
            'startup' => !empty($startup) ? intval($startup) : 0,
            'base' => !empty($base) ? intval($base) : 0
        ];
        return $data;
    }

    /**
     * 首页顶部概览
     * @return array
     */
    public function getSummary()
    {
        $adminid = adminId();
        if ($adminid == 2) {
            $date = date('Y-m-d');
            $query = $this->db->get_where('analytics_daily', ['date' => $date]);
            $row = $query->row_array();
            $data = [
                'order' => !empty($row) ? intval($row['appOrders']) + intval($row['wxOrders']) : 0,
                'install' => !empty($row) ? intval($row['installTimes']) : 0,
                'user' => !empty($row) ? intval($row['appUsers']) + intval($row['wxUsers']) : 0,
                'bbs' => !empty($row) ? intval($row['bbsIncr']) : 0
            ];
        } else {
            $date = date('Y-m-d');
            $query = $this->db->get_where('analytics_daily', ['date' => $date]);
            $row = $query->row_array();
            $data = [
                'order' => !empty($row) ? intval($row['vAppOrders']) + intval($row['vWxOrders']) + intval($row['appOrders']) + intval($row['wxOrders']) : 0,
                'install' => !empty($row) ? intval($row['installTimes']) + intval($row['vInstallTimes']) : 0,
                'user' => !empty($row) ? intval($row['appUsers']) + intval($row['wxUsers']) + intval($row['vAppUsers']) + intval($row['vWxUsers']) : 0,
                'bbs' => !empty($row) ? intval($row['bbsIncr']) + intval($row['vBbsIncr']) : 0
            ];
        }

        return $data;
    }

    /**
     * @param $days
     * @return mixed
     */
    public function getSummaryChart($days)
    {

        $summaryChart = $this->_getSummaryChart();
        $userChart = $this->_getUserChart();
        $appChart = $this->_getAppChart();


        return [
            'label' => [
                'summary' => $summaryChart['label'],
                'app' => $appChart['label'],
            ],
            'data' => [
                'summary' => $summaryChart['data'],
                'app' => $appChart['data'],
                'user' => $userChart
            ]
        ];
    }

    private function _getAppChart()
    {
        $days = 7;
        $startTime = strtotime("-{$days} day");
        $startDate = date('Y-m-d', $startTime);

        $query = $this->db->get_where('analytics_daily', ['date >= ' => $startDate]);
        $rows = $query->result_array();

        $dailyRows = [];
        if (!empty($rows)) foreach($rows as $item) {
            $date = $item['date'];
            $dailyRows[$date] = $item;
        }

        $labels = [];
        $data = [];
        $data['android'] = [];
        $data['ios'] = [];

        $adminid = adminId();

        for($i = $days; $i >= 0; $i--) {
            $time = strtotime("-{$i} day");
            $date = date('Y-m-d', $time);
            $labels[] = date('m-d', $time);
            if ($adminid == 2) {
                $data['ios'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['iosInstallTimes']) : 0;
                $data['android'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['androidInstallTimes']) : 0;
            } else {
                $data['ios'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['iosInstallTimes']) + intval($dailyRows[$date]['vIosInstallTimes']) : 0;
                $data['android'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['androidInstallTimes']) + intval($dailyRows[$date]['vAndroidInstallTimes']) : 0;
            }
        }

        return [
            'label' => $labels,
            'data' => $data
        ];
    }

    /**
     * @return array
     */
    private function _getUserChart()
    {
        $result = [];
        $adminid = adminId();
        if ($adminid == 2) {
            $db = clone($this->db);
            $db->where('virtual', 0);
            $db->where('source', 'wx');
            $result['wx'] = $db->count_all_results('user');
            $db->where('source', 'ios');
            $result['ios'] = $db->count_all_results('user');
            $db->where('source', 'android');
            $result['android'] = $db->count_all_results('user');
        } else {
            $db = clone($this->db);
            $db->where('source', 'wx');
            $result['wx'] = $db->count_all_results('user');
            $db->where('source', 'ios');
            $result['ios'] = $db->count_all_results('user');
            $db->where('source', 'android');
            $result['android'] = $db->count_all_results('user');
        }
        return $result;
    }

    private function _getSummaryChart()
    {
        $days = 15;
        $startTime = strtotime("-{$days} day");
        $startDate = date('Y-m-d', $startTime);

        $query = $this->db->get_where('analytics_daily', ['date >= ' => $startDate]);
        $rows = $query->result_array();

        $dailyRows = [];
        if (!empty($rows)) foreach($rows as $item) {
            $date = $item['date'];
            $dailyRows[$date] = $item;
        }

        $labels = [];
        $data = [];
        $data['order'] = [];
        $data['app'] = [];
        $data['user'] = [];
        $data['bbs'] = [];

        $adminid = adminId();

        for($i = $days; $i >= 0; $i--) {
            $time = strtotime("-{$i} day");
            $date = date('Y-m-d', $time);
            $labels[] = date('m-d', $time);
            if ($adminid == 2) {
                $data['order'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['appOrders']) + intval($dailyRows[$date]['wxOrders']) : 0;
                $data['app'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['installTimes']) : 0;
                $data['user'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['appUsers']) + intval($dailyRows[$date]['wxUsers']) : 0;
                $data['bbs'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['bbsIncr']) : 0;
            } else {
                $data['order'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['vWxOrders']) + intval($dailyRows[$date]['vAppOrders']) + intval($dailyRows[$date]['appOrders']) + intval($dailyRows[$date]['wxOrders']) : 0;
                $data['app'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['installTimes']) + intval($dailyRows[$date]['vInstallTimes']) : 0;
                $data['user'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['vWxUsers']) + intval($dailyRows[$date]['vAppUsers']) + intval($dailyRows[$date]['appUsers']) + intval($dailyRows[$date]['wxUsers']) : 0;
                $data['bbs'][] = isset($dailyRows[$date]) ? intval($dailyRows[$date]['bbsIncr']) + intval($dailyRows[$date]['vBbsIncr']) : 0;
            }
        }

        return [
            'label' => $labels,
            'data' => $data
        ];
    }
}